package com.ml.mall.entity.variant;

import java.math.BigDecimal;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;

import java.util.Date;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableField;

import java.io.Serializable;

import com.ml.mall.entity.deduct.SysADeduct;
import com.ml.mall.utils.OrderPriceUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 *
 * </p>
 *
 * @author kyyang
 * Created 2021-05-19
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_product_variant")
@ApiModel(value = "SysProductVariant对象", description = "")
public class SysProductVariant implements Serializable {

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;


    @TableField(value = "ver_id")
    private Integer verId;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间")
    @TableField(value = "create_at", fill = FieldFill.INSERT)
    private Date createAt;

    /**
     * 更新时间
     */
    @ApiModelProperty(value = "更新时间")
    @TableField(value = "update_at", fill = FieldFill.INSERT_UPDATE)
    private Date updateAt;

    /**
     * 是否删除
     */
    @ApiModelProperty(value = "是否删除")
    @TableField(value = "is_delete", fill = FieldFill.INSERT)
    @TableLogic(value = "0", delval = "1")
    private Integer isDelete;

    /**
     * 类别用于商城搜索
     */
    @ApiModelProperty(value = "类别用于商城搜索")
    @TableField("category_id")
    private String categoryId;

    /**
     * 供应商ID
     */
    @ApiModelProperty(value = "供应商ID")
    @TableField("supplier_id")
    private Long supplierId;

    /**
     * 产品描述
     */
//    @ApiModelProperty(value = "产品描述")
//    @TableField("description")
//    private String description;

    /**
     * 品牌
     */
//    @ApiModelProperty(value = "品牌")
//    @TableField("brand")
//    private String brand;

    /**
     * 销量
     */
    @ApiModelProperty(value = "销量")
    @TableField("sale_num")
    private Integer saleNum;

    /**
     * 审核状态：0->未审核；1->审核通过
     */
    @ApiModelProperty(value = "审核状态：0->未审核；1->审核通过")
    @TableField("verify_status")
    private Integer verifyStatus;

    /**
     * 1->免费包邮；2;不支持退货；3：退换无忧
     */
    @ApiModelProperty(value = "1->免费包邮；2;不支持退货；3：退换无忧")
    @TableField("service_ids")
    private Integer serviceIds;

    /**
     * 产品ID
     */
    @ApiModelProperty(value = "产品ID")
    @TableField("product_id")
    private Long productId;

    /**
     * 子商品ID
     */
    @ApiModelProperty(value = "子商品ID")
    @TableField("variant_id")
    private Long variantId;

    /**
     * 出售商品名称：瓶；箱
     */
    @ApiModelProperty(value = "出售商品名称：瓶；箱")
    @TableField("variant_name")
    private String variantName;

    /**
     * 价格
     */
    @ApiModelProperty(value = "价格")
    @TableField("price")
    private BigDecimal price;

    /**
     * 出售价格+税+系统抽成
     */
    @ApiModelProperty(value = "售价提成百分比")
    @TableField("commission_rate")
    private BigDecimal commissionRate;

    /**
     * 是否包含税
     */
    @ApiModelProperty(value = "是否包含税")
    @TableField("includ_tax")
    private Integer includTax;

    /**
     * 状态：0正常；1下线
     */
    @ApiModelProperty(value = "状态：0正常；1下线")
    @TableField(value = "`state`", fill = FieldFill.INSERT)
    private Integer state;

    @ApiModelProperty(value = "标题")
    @TableField("title")
    private String title;

    @ApiModelProperty(value = "关键字搜索")
    @TableField("search_key_word")
    private String searchKeyWord;

    @ApiModelProperty(value = "权重")
    @TableField("search_weight")
    private String searchWeight;

    /**
     * 数据生效日期
     */
    @ApiModelProperty(value = "数据生效日期")
    @TableField("effect_dt")
    private Date effectDt;

    /**
     * 数据失效日期
     */
    @ApiModelProperty(value = "数据失效日期")
    @TableField("expiry_dt")
    private Date expiryDt;

    //只用于计算购物车返回时促销的售价
    @TableField(exist = false)
    private BigDecimal sellPrice=null;

    /**
     * 计算提成之后的价格
     * @param
     * @return void
     * @author Daizh
     * @date 2021/9/10 10:53
     */
    public void changePrice(SysADeduct curDeduct) {
        BigDecimal deductRate = BigDecimal.ZERO;
        Integer type = 0;
        if (null != curDeduct) {
            deductRate = curDeduct.getDeductRate();
            type = curDeduct.getCentType();
        }
        //计算提成
        //计算提成：溢出价全部在原价的基础上
        BigDecimal increasMoney = this.commissionRate==null?this.price.multiply(deductRate):this.price.multiply(this.commissionRate);
        if (null != this.sellPrice) {
            //使用单独抽成还是系统抽成
//            BigDecimal increasMoney = this.commissionRate==null?this.sellPrice.multiply(deductRate):this.sellPrice.multiply(this.commissionRate);
            this.sellPrice = this.sellPrice.add(increasMoney).setScale(2, BigDecimal.ROUND_HALF_DOWN);
            this.sellPrice = OrderPriceUtils.getOrderPriceByCentType(type,  this.sellPrice);
        }
        //计算真实价格的提成
        this.price = this.price.add(increasMoney).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        this.price = OrderPriceUtils.getOrderPriceByCentType(type, this.price);
    }
}
